<?php
use yii\bootstrap\ActiveForm;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/daterangepicker/daterangepicker.css" type="text/css">
<style>

    .dataTable>tbody>tr>td {
        vertical-align: middle;
        font-size: 0.8em;
        padding: 8px 18px;
    }
    .dataTable>thead>tr>th {
        vertical-align: middle;
        font-size: 0.8em;
    }

    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    #table_id_example th, td {
        white-space: nowrap;
    }
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    物料名称: <input class="depart-input" id="searchName" style="width: 160px" type="text">&nbsp;
                    物料编号: <input class="depart-input" id="searchCode" style="width: 160px" type="text">&nbsp;
                    批 次 号: <input class="depart-input" id="searchBatch" style="width: 160px" type="text">
                    仓库：<select id="warehouseId" name="warehouseId" class="depart-input" style="width: 200px">
                        <option value="">选择仓库</option>
                        <?php foreach($warehouse as $value) { ?>
                            <option value="<?= $value['id']?>"><?= $value['warehouseName']?></option>
                        <?php }?>
                    </select>&nbsp;&nbsp;
                    供应商: <select id="supplierId" class="depart-input" style="width: 200px">
                        <option value="">全部</option>
                        <?php foreach($supplier as $value){ ?>
                            <option value="<?= $value['id']?>"><?= $value['supplierName']?></option>
                        <?php }?>
                    </select>
                    <br><br>
                    生产日期: <input class="depart-input" id="searchProDay" style="width: 200px" type="text">
                    <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#searchProDay').val('')">清空</button>&nbsp;
                    保质期: <input class="depart-input" id="searchSafeDay" style="width: 200px" type="text">
                    <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#searchSafeDay').val('')">清空</button>&nbsp;
                    <button type="button" class="btn btn-primary depart-modal" id="select_button" style="width: 80px;">查询</button>&nbsp;
                    <button type="button" class="btn btn-warning depart-modal" id="reset_button" style="width: 80px;">重置</button>
                    <button type="button" class="btn btn-primary depart-modal" id="export_button" style="width: 80px;">导出</button>
                </div>
                <div class="box-body">
                    <table id="table_id_example" class="display">
                        <thead>
                        <tr>
                            <th style="width: 6%">操作</th>
                            <th style="width: 6%">物料ID</th>
                            <th style="width: 7%">物料名称</th>
                            <th style="width: 7%">供应商</th>
                            <th style="width: 7%">仓库</th>
                            <th style="width: 5%">批次号</th>
                            <th style="width: 5%">总数量</th>
                            <th style="width: 6%">冻结数量</th>
                            <th style="width: 6%">可用数量</th>
                            <th style="width: 5%">单价</th>
                            <th style="width: 5%">金额</th>
                            <th style="width: 6%">生产日期</th>
                            <th style="width: 6%">保质期</th>
                            <th style="width: 6%">规格描述</th>
                            <th style="width: 6%">包装方式</th>
                            <th style="width: 6%">采购单位</th>
                            <th style="width: 6%">订货单位</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 移库 -->
<div class="modal fade" id="move-stock-modal" style="background: rgba(236, 230, 230, 0.3);" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">库存转移</h4>
            </div>
            <div class="modal-body">
                <div class="box-body">
                    <div class="col-md-12">
                        <label class="col-sm-4 control-label">仓库</label>
                        <div class="col-sm-6" id="s_whName">
                        </div>
                    </div>
                    <div class="col-md-12">
                        <label class="col-sm-4 control-label">物料</label>
                        <div class="col-sm-6" id="s_maName">
                        </div>
                    </div>
                    <div class="col-md-12">
                        <label class="col-sm-4 control-label">批次</label>
                        <div class="col-sm-6" id="s_batch">
                        </div>
                    </div>
                    <div class="col-md-12">
                        <label class="col-sm-4 control-label">可用库存</label>
                        <div class="col-sm-6" id="s_qty">
                        </div>
                    </div>
                    <div class="col-md-12">
                        <label class="col-sm-4 control-label">转入仓库</label>
                        <div class="col-sm-6">
                            <select id="s_towhId" class="form-control">
                                <option value="">选择仓库</option>
                                <?php foreach($warehouse as $value) { ?>
                                    <option value="<?= $value['id']?>"><?= $value['warehouseName']?></option>
                                <?php }?>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-12" style="margin-top: 5px">
                        <label class="col-sm-4 control-label">转入数量</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="s_toQty">
                            <input type="hidden" id="s_stockId">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="move-stock-btn">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- 固定js -->
<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<script type="text/javascript" src="/static/plugins/treeTable/jquery.treetable.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="../static/plugins/moment/moment.min.js"></script>
<script type="text/javascript" src="../static/plugins/daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="../static/plugins/datepicker/bootstrap-datepicker.js"></script>

<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script>
    var dateTable = null;
    $(function () {
        $('#searchProDay, #searchSafeDay').daterangepicker({
            language: 'zh-CN',
            timePicker: false,
            timePickerIncrement: 30,
            format: 'YYYY-MM-DD',
            locale: {
                format: 'YYYY-MM-DD',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            }
        });
        $('#searchProDay, #searchSafeDay').val('');

        dateTable = $('#table_id_example').DataTable({
            dom: '<"top">rt<"bottom"flip><"clear">',
            language: {
                "sUrl": '../static/plugins/datatables/language.json'
            },
            "searching": false,
            "processing": true,
            "serverSide": true,
            "scrollCollapse": true,
            "scrollX": true,
            ajax: {
                url: '/stock/get-materiel-stock',
                type: 'post',
                data: function (d) {
                    d.searchName = $('#searchName').val();
                    d.searchCode = $('#searchCode').val();
                    d.searchBatch = $('#searchBatch').val();
                    d.warehouseId = $('#warehouseId').val();
                    d.supplierId = $('#supplierId').val();
                    d.searchProDay = $('#searchProDay').val();
                    d.searchSafeDay = $('#searchSafeDay').val();
                },
                error: getError
            },
            aoColumns: [
                {"data": "", "bSortable": false},
                {"data": "materielId", "bSortable": false},
                {"data": "materielName", "bSortable": false},
                {"data": "supplierName", "bSortable": false},
                {"data": "warehouseName", "bSortable": false},
                {"data": "batch", "bSortable": true},
                {"data": "total", "bSortable": true},
                {"data": "freeze", "bSortable": true},
                {"data": "qty", "bSortable": true},
                {"data": "price", "bSortable": false},
                {"data": "amount", "bSortable": false},
                {"data": "productDate", "bSortable": true},
                {"data": "safeDate", "bSortable": true},
                {"data": "specDesc", "bSortable": false},
                {"data": "packDesc", "bSortable": false},
                {"data": "purchaseUnit", "bSortable": false},
                {"data": "orderUnit", "bSortable": false},
            ],
            columnDefs: [
                {
                    "targets": [0],
                    "orderable": false,
                    "searchable": false,
                    "render": function (data, type, full) {
                        return '<a href=\'javascript:showMove(' + full.id + ',"' + full.materielName + '","' + full.warehouseName + '","' + full.warehouseId + '","' + full.batch + '","' + full.qty + '")\'>移库</a>'
                    }
                },
                {
                    "targets": [7],
                    "orderable": false,
                    "searchable": false,
                    "render": function (data, type, full) {
                        if (data > 0) {
                            return '<span style="color: red">' + data + '</span>'
                        }
                        return data;
                    }
                }
            ],
            order: [[8, "asc"]]
        });

        $('#select_button').on('click', function () {
            dateTable.ajax.reload();
        });

        $('#reset_button').on('click', function () {
            $('#searchName').val('');
            $('#searchCode').val('');
            $('#searchBatch').val('');
            $('#warehouseId').val('');
            $('#supplierId').val('');
            $('#searchProDay').val('');
            $('#searchSafeDay').val('');
            dateTable.ajax.reload();
        });

        $('#move-stock-btn').on('click', function () {
            var stockId = $('#s_stockId').val();
            var toWhId = $('#s_towhId').val();
            var toQty = $('#s_toQty').val();
            var fromQty = $('#s_qty').html();

            if (toWhId == '') {
                $.jBox.tip('请选择转入的仓库', '错误');
                return;
            }

            if ($.trim(toQty) == '') {
                $.jBox.tip('请输转入库存数量', '错误');
                return;
            } else if (toQty <= 0 || !$.isNumeric(toQty)) {
                $.jBox.tip('请输正确的转入库存数量', '错误');
                return;
            }
            if(parseFloat(toQty) > parseFloat(fromQty)) {
                $.jBox.tip('转出数量不可大于可用库存', '错误');
                return;
            }

            $.post('/stock/move-stock', {stockId: stockId, toWhId: toWhId, toQty: toQty},
                function (data) {
                    if (data.code == 200) {
                        $.jBox.tip('操作成功', 'success');
                        $('#move-stock-modal').modal('hide');

                        window.setTimeout(function () {
                            dateTable.draw(false);
                        }, 1000);
                    } else {
                        $.jBox.error(data.val, '错误');
                    }
                },
                'json'
            );
        });

        $('#export_button').on('click',function () {
            var params = 'searchName=' + $('#searchName').val();
            params += '&searchCode=' + $('#searchCode').val();
            params += '&searchBatch=' + $('#searchBatch').val();
            params += '&warehouseId=' + $('#warehouseId').val();
            params += '&supplierId=' + $('#supplierId').val();
            params += '&searchProDay=' + $('#searchProDay').val();
            params += '&searchSafeDay=' + $('#searchSafeDay').val();
            params += '&v=' + Math.random();

            location.href = encodeURI('/stock/export?' + params);
        })
    });

    //转移库存
    function showMove(id,maName,whName,whId,batch,qty) {
        $('#s_stockId').val(id);
        $('#s_whName').html(whName);
        $('#s_maName').html(maName);
        $('#s_batch').html(batch);
        $('#s_qty').html(qty);
        $('#s_towhId').val('');
        $('#s_towhId').find('option[disabled]').removeAttr('disabled');
        $('#s_towhId').find('option[value='+whId+']').attr('disabled',true);
        $('#move-stock-modal').modal('show');
    }

</script>